<?php
class IndexController extends CController {
	public function actionIndex() {
		/** All data view in the page */
		$sectionSize = 10;

//		$postsByTopic = Posts::model()->section()->findAll();
		
		$slides = Slides::model()->getAll();

		$posts = array();
		$categoryIds = array(
			'咨询案例' => array(44, 45),
			'企业 EAP' => array(35, 36, 37, 38),
			'咨询专题' => array(39, 40, 41, 42),
		);
		foreach($categoryIds as $k => $v) {
			$posts[$k]['meta'] = TermsTaxonomy::model()->findByPk($v[0]);
			$posts[$k]['rows'] = Posts::model()->published()->section()->findByCategoryId(implode(',', $v));
		}
		/** Render to view */
		// Convert slides' data into JS's array format
		$js = "var imgPath = new Array();\nvar linkTo = new Array();\nvar descr = new Array();\n";
		foreach($slides as $k => $slide) {
			$js .= "imgPath[{$k}] = '{$slide->imgPath}';\n";
			$js .= "linkTo[{$k}] = '{$slide->linkTo}';\n";
			$js .= "descr[{$k}] = '{$slide->slideDescr}';\n";
		}
		$links = Links::model()->findAll();
		Yii::app()->clientScript->registerScript('slidesArrayInit', $js, CClientScript::POS_END);
		Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/js/oSlides.js', CClientScript::POS_END);
		Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/js/index.js', CClientScript::POS_END);
		$this->pageTitle = '广州心理咨询网  |广州心理医生|婚姻情感|强迫思维|催眠|抑郁症|网瘾戒除|厌学';
		$this->render('index', array(
//			'postsByTopic' => $postsByTopic,
			'sectionSize' => $sectionSize,
//			'psychologist' => $psychologist,
			'posts' => $posts,
			'categoryIds' => $categoryIds,
			'slides' => $slides,
			'links' => $links
		));
	}

	public function actionSection() {
		$this->widget('IndexSection');
	}
	
	public function actionError() {
	    $error = Yii::app()->errorHandler->error;
    	if ($error) {
    		$this->setPageTitle('出错啦 - 广州心理咨询网');
    		$this->render('error', array('error' => $error));
    	} else {
    		throw new CHttpException(404, 'Page not found.');
    	}
	}
//	public function actionTest() {
//		Yii::import('application.extensions.mailer.Emailer');
//		$mailer = new Emailer;
//		$mailer->IsSMTP(); // 使用SMTP
//		$mailer->SMTPSecure = "ssl"; // 设置连接服务器前缀
//		$mailer->Host = 'smtp.gmail.com';      // GMAIL的SMTP
//		$mailer->Port = 465;                   // GMAIL的SMTP端口号
//		$mailer->SMTPAuth = true; // 开启SMTP认证
//		$mailer->Username = 'adrian4ding@gmail.com'; // GMAIL用户名
//		$mailer->Password = "dcbtpz22"; // GMAIL密码
//		$email = "26740748@qq.com"; // 收件人
//		$name = "丁超兵"; // 收件人名称
//		$mailer->From = Yii::app()->params->adminEmail;
//		$mailer->FromName = "广州心理咨询网";
//		$mailer->AddAddress($email, $name);
//		$mailer->AddReplyTo(Yii::app()->params->adminEmail, "Webmaster");
//		$mailer->WordWrap = 50; // 设定 word wrap
//		$mailer->IsHTML(true); // 以HTML发送
//		$mailer->CharSet = Yii::app()->params->emailBodyCharset;
//		$mailer->Subject = "欢迎来到广州心理咨询网！";
//		$mailer->Body = "Hi, This is the HTML BODY "; //HTML Body
//		$mailer->AltBody = "This is the body when user views in plain text format"; //纯文字时的Body
//		echo '<pre>';
//		print_r($mailer);
//		if(!$mailer->Send()) {
//		echo "Mailer Error: " . $mailer->ErrorInfo;
//		}
//		else
//		{
//		echo "Message has been sent";
//}
//	}
}
